package com.zfj.TreeSet;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author zfj
 * @date 2022/3/7 -16:34
 * 使用TreeSet集合实现字符串按照长度排序
 * Comparator接口实现定制比较
 *
 */
public class Demo4 {
	public static void main(String[] args) {
		TreeSet<String> treeSet=new TreeSet<>(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				int n1=o1.length()-o2.length();
				int n2=o1.compareTo(o2);
				return n1==0?n2:n1;
			}
		});//添加元素
		treeSet.add("helloworld");
		treeSet.add("hello");
		treeSet.add("zfj");
		treeSet.add("l");
		treeSet.add("zhangfujing");
		System.out.println(treeSet.toString());


	}
}
